package com.dg.lecheng.api.user.service;

import com.dg.lecheng.api.user.model.InfoRole;
import com.dg.lecheng.api.user.model.InfoRoleAuth;
import com.dg.lecheng.api.user.repo.InfoRoleAuthDao;
import com.dg.lecheng.base.core.service.CrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InfoRoleAuthService extends CrudService<InfoRoleAuthDao, InfoRoleAuth, Integer> {
    @Autowired
    private InfoRoleService infoRoleService;
    @Autowired
    private InfoAuthService infoAuthService;

    public int getCount() {
        return curdRepo.getCount();
    }

    public List<InfoRoleAuth> getByRoleId(Integer roleId) {
        return curdRepo.getByRoleId(roleId);
    }

    /**
     * 根据roleId删除对应关系
     * @param roleId
     */
    public void removeByRoleId(int roleId){
        curdRepo.removeByRoleId(roleId);
    }

    /**
     * 根据authId删除对应关系
     * @param authId
     */
    public void removeByAuthId(int authId){
        curdRepo.removeByAuthId(authId);
    }

    /**
     * 批量添加
     * @param list
     */
    public void addList(List<Map> list){
        curdRepo.addList(list);
    }

    /**
     * 保存当前角色的关联资源
     * 1. delete
     * 2. add
     * @param map
     */
    @Transactional("apiTM")
    public void addRoleAndAuthList(Map<String,Object> map) {
        if(map == null){
            return ;
        }

        int roleId = (int)map.get("roleId");
        List<Map> roleAuths = (List<Map>)map.get("roleAuths");

        if(roleId == 0 || roleAuths == null || roleAuths.size() == 0){
            return ;
        }

        // 删除该roleId的资源
        curdRepo.removeByRoleId(roleId);
        // 追加选中的资源
        curdRepo.addList(roleAuths);

    }


    /**
     * 保存角色-资源
     * @param roleId
     * @param authIds
     */
    @Transactional
    public void addRoleAndAuthList(Integer roleId, List<Integer> authIds) {
        curdRepo.removeByRoleId(roleId);
        curdRepo.saveRoleAuth(roleId,authIds);
    }

    /**
     * 查找角色和资源
     * @return
     */
    public Map<String,Object> findRoleAndAuth(int userId,int roleId){
        List<InfoRole> roles = infoRoleService.findAll();

        List<Map<String,Object>> auths = new ArrayList<>();
        if(roleId == 0){
            auths = infoAuthService.getListForZtree();
        }else{
            auths = infoAuthService.findRoleAuthByRoleIdAndUserId(userId,roleId);
        }
        if(roles != null && roles.size() > 0 && auths != null && auths.size() > 0){
            Map result = new HashMap();
            result.put("roles",roles);
            result.put("auths",auths);
            return result;
        }
        return null;
    }


    /**
     * 根据角色Id查找对应的authId
     * @param roleId
     * @return
     */
    public List<Map> findRoleAuthByRoleId(int roleId){
        return curdRepo.findRoleAuthByRoleId(roleId);
    }

    /**
     * 根据角色Id查找对应的资源信息
     * @param roleId
     * @return
     */
    public List<Map> findAuthByRoleId(int roleId){
        return curdRepo.findAuthByRoleId(roleId);
    }

}